/**
 * 
 */
package com.vector.ding.web.service.common;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.vector.ding.commons.util.StringUtil;
import com.vector.ding.commons.util.collection.CollectionUtil;
import com.vector.ding.dao.mybatis.mapper.JcxxpfInfoMapper;
import com.vector.ding.dao.mybatis.model.GqtzgsBizInfo;
import com.vector.ding.dao.mybatis.model.JcxxpfInfo;
import com.vector.ding.dao.mybatis.model.JcxxpfInfoExample;
import com.vector.ding.web.service.bizinfo.GqtzgsBizInfoMgrService;

/**
 * @author vector
 *
 */
@Service
public class BaseInfoScoreService {
	
	@Autowired
	private JcxxpfInfoMapper baseScoreMapper;
	@Autowired
	private GqtzgsBizInfoMgrService bizService;
	
	public JcxxpfInfo getOne(String companyName) {
		List<JcxxpfInfo> list = getList(companyName);
		return CollectionUtil.isEmpty(list) ? null : list.get(0);
	}
	
	public JcxxpfInfo getOne(String companyName, int belongYear) {
		if (StringUtil.isEmpty(companyName))
			return null;
		
		JcxxpfInfoExample example = new JcxxpfInfoExample();
		example.or()
			.andCompanyNameEqualTo(companyName)
			.andBelongYearEqualTo(belongYear);
		List<JcxxpfInfo> list = baseScoreMapper.selectByExample(example);
		return CollectionUtil.isEmpty(list) ? null : list.get(0);
	}

	public List<JcxxpfInfo> getList(String companyName) {
		if (StringUtil.isEmpty(companyName))
			return Collections.emptyList();
		
		JcxxpfInfoExample example = new JcxxpfInfoExample();
		example.or().andCompanyNameEqualTo(companyName);
		List<JcxxpfInfo> list = baseScoreMapper.selectByExample(example);
		if (CollectionUtil.isEmpty(list))
			return Collections.emptyList();
		return list;
	}
	
	public BigDecimal getScore(String companyName, int belongYear) {
//		JcxxpfInfo info = getOne(companyName, belongYear);
//		return info == null ? null : info.getBaseInfoGrade();
		
		GqtzgsBizInfo bizInfo = bizService.getBizInfo(companyName, belongYear);
		if (bizInfo == null)
			return new BigDecimal(0);
		
		BigDecimal score = new BigDecimal(0);
		BigDecimal revenueGrade = bizInfo.getRevenueGrade();
		if (revenueGrade != null)
			score = score.add(revenueGrade);
		return score;
	}
	
	public BigDecimal getScore(String companyName) {
//		List<JcxxpfInfo> list = getList(companyName);
		List<GqtzgsBizInfo> list = bizService.getBizInfos(companyName);
		if (CollectionUtil.isEmpty(list))
			return new BigDecimal(0);
		
		BigDecimal score = new BigDecimal(0);
		for (GqtzgsBizInfo info: list) {
			BigDecimal revenueGrade = info.getRevenueGrade();
			if (revenueGrade != null)
				score = score.add(revenueGrade);
		}
		return score;
	}
	
}